// 网银APP软件客户端
#include "../_public.h"

CTcpClient TcpClient;

bool srv001();			// 登录
bool srv002();			// 账户余额

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		printf("Using:./demo07 ip port\nExample:./demo07 127.0.0.1 5005\n\n");
		return -1;
	}

	// 向服务端发起连接请求
	if( TcpClient.ConnectToServer( argv[1], atoi(argv[2]) ) == false){
		printf("TcpClient.ConnectToServer(%s, %s) failed.\n", argv[1], argv[2]);
		return -1;
	}

	if (srv001() == false){
		printf("srv001() failed.\n");
		return -1;
	}
	if (srv002() == false){
		printf("srv002() failed.\n");
		return -1;
	}
	
}

// 登录
bool srv001(){
	char buffer[1024];

	SPRINTF(buffer, sizeof(buffer),"<srvcode>1</srvcode><tel>123456</tel><password>123456</password>");
	if ( TcpClient.Write(buffer) == false ) // 向服务端发送请求报文。
	{
		return false;
	}

	memset(buffer, 0, sizeof(buffer));
	if ( TcpClient.Read(buffer) == false ) // 接收服务端的回应报文。
	{
		return false;
	}
	printf("接收：%s\n", buffer);

	// 解析服务端返回的xml
	int iretcode = -1;
	GetXMLBuffer(buffer, "retcode", &iretcode);
	if(iretcode != 0){
		printf("登录失败\n");
		return false;
	}
	printf("登录成功\n");
	return true;
}

// 账户余额
bool srv002(){
	char buffer[1024];

	SPRINTF(buffer, sizeof(buffer),"<srvcode>2</srvcode><cardid>111111</cardid>");
	if ( TcpClient.Write(buffer) == false ) // 向服务端发送请求报文。
	{
		return false;
	}
	printf("发送：%s\n",buffer);

	memset(buffer, 0, sizeof(buffer));
	if ( TcpClient.Read(buffer) == false ) // 接收服务端的回应报文。
	{
		return false;
	}
	printf("接收：%s\n", buffer);

	// 解析服务端返回的xml
	int iretcode = -1;
	GetXMLBuffer(buffer, "retcode", &iretcode);
	if(iretcode != 0){
		printf("查询余额失败\n");
		return false;
	}

	double amount = 0;
	GetXMLBuffer(buffer, "amount", &amount);
	printf("查询成功(%.2f)\n", amount);
	return true;
}
